/**
 * OWASP GoatDroid Project
 * 
 * This file is part of the Open Web Application Security Project (OWASP)
 * GoatDroid project. For details, please see
 * https://www.owasp.org/index.php/Projects/OWASP_GoatDroid_Project
 *
 * Copyright (c) 2011 - The OWASP Foundation
 * 
 * GoatDroid is published by OWASP under the GPLv3 license. You should read and accept the
 * LICENSE before you use, modify, and/or redistribute this software.
 * 
 * @author Jack Mannino, nVisium Security (https://www.nvisiumsecurity.com)
 * @created 2011
 */
package org.owasp.goatdroid.gui.view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.GridBagLayout;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import java.awt.Color;
import javax.swing.JTree;
import java.awt.GridBagConstraints;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JEditorPane;
import javax.swing.JButton;
import org.owasp.goatdroid.gui.Constants;
import org.owasp.goatdroid.gui.LessonTreeBuilder;
import org.owasp.goatdroid.gui.config.Config;
import org.owasp.goatdroid.gui.emulator.Emulator;
import org.owasp.goatdroid.gui.emulator.EmulatorRunnable;
import org.owasp.goatdroid.gui.exception.CorruptConfigException;
import org.owasp.goatdroid.gui.webservices.WebServices;
import java.awt.ComponentOrientation;
import java.awt.Point;
import java.awt.Rectangle;

public class HomeFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jFileMenu = null;
	private JMenu jConfigurationMenu = null;
	private JMenu jAboutMenu = null;
	private JMenuItem jExitMenuItem = null;
	private JSplitPane jSplitPane = null;
	private JScrollPane jLessonSelectorScrollPane = null;
	private JScrollPane jWorkAreaScrollPane = null;
	private JPanel jPanel = null;
	private JTabbedPane jLessonsSolutionsTabbedPane = null;
	private JPanel jLessonTab = null;
	private JPanel jSolutionTab = null;
	private JTree jLessonTree = null;
	private JEditorPane jLessonInfoEditorPane = null;
	private JButton jSolutionButton = null;
	private JEditorPane jSolutionEditorPane = null;
	private JMenuItem jConfigurationEditMenuItem = null;
	private JMenuItem jAboutMenuItem = null;
	private JMenu jToolsMenu = null;
	private JMenuItem jToolsSendLocationMenuItem = null;
	private JSplitPane jLessonSplitPane = null;
	private JPanel jLessonStartPanel = null;
	private JButton jStartWebServiceButton = null;
	private JButton jStartInEmulatorButton = null;
	static private Config config;
	private JButton jPushAppButton = null;

	/**
	 * This is the default constructor
	 */
	public HomeFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1000, 750);
		this.setPreferredSize(new Dimension(400, 560));
		this.setResizable(true);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("OWASP GoatDroid");
		config = new Config();
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setPreferredSize(new Dimension(0, 0));
			jContentPane.setLayout(null);
			jContentPane
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			jContentPane.add(getJSplitPane(), null);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJFileMenu());
			jJMenuBar.add(getJConfigurationMenu());
			jJMenuBar.add(getJToolsMenu());
			jJMenuBar.add(getJAboutMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jFileMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJFileMenu() {
		if (jFileMenu == null) {
			jFileMenu = new JMenu();
			jFileMenu.setText("File");
			jFileMenu.add(getJExitMenuItem());
		}
		return jFileMenu;
	}

	/**
	 * This method initializes jConfigurationMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJConfigurationMenu() {
		if (jConfigurationMenu == null) {
			jConfigurationMenu = new JMenu();
			jConfigurationMenu.setText("Configure");
			jConfigurationMenu.add(getJConfigurationEditMenuItem());
		}
		return jConfigurationMenu;
	}

	/**
	 * This method initializes jAboutMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJAboutMenu() {
		if (jAboutMenu == null) {
			jAboutMenu = new JMenu();
			jAboutMenu.setText("About GoatDroid");
			jAboutMenu.add(getJAboutMenuItem());
		}
		return jAboutMenu;
	}

	/**
	 * This method initializes jExitMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJExitMenuItem() {
		if (jExitMenuItem == null) {
			jExitMenuItem = new JMenuItem();
			jExitMenuItem.setText("Exit");
			jExitMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseReleased(java.awt.event.MouseEvent e) {
					System.exit(0);
				}
			});
		}
		return jExitMenuItem;
	}

	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setEnabled(false);
			jSplitPane.setDividerLocation(200);
			jSplitPane.setContinuousLayout(true);
			jSplitPane.setResizeWeight(1.0D);
			jSplitPane.setPreferredSize(new Dimension(0, 0));
			jSplitPane.setLocation(new Point(0, 0));
			jSplitPane.setSize(new Dimension(1000, 700));
			jSplitPane.setLeftComponent(getJLessonSelectorScrollPane());
			jSplitPane.setRightComponent(getJWorkAreaScrollPane());
			jSplitPane.setOneTouchExpandable(true);

		}
		return jSplitPane;
	}

	/**
	 * This method initializes jLessonSelectorScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJLessonSelectorScrollPane() {
		if (jLessonSelectorScrollPane == null) {
			jLessonSelectorScrollPane = new JScrollPane();
			jLessonSelectorScrollPane.setPreferredSize(new Dimension(800, 512));
			jLessonSelectorScrollPane.setViewportView(getJLessonTree());
		}
		return jLessonSelectorScrollPane;
	}

	/**
	 * This method initializes jWorkAreaScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJWorkAreaScrollPane() {
		if (jWorkAreaScrollPane == null) {
			jWorkAreaScrollPane = new JScrollPane();
			jWorkAreaScrollPane
					.setViewportView(getJLessonsSolutionsTabbedPane());
		}
		return jWorkAreaScrollPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBounds(new Rectangle(1000, 700, 0, 6));
		}
		return jPanel;
	}

	/**
	 * This method initializes jLessonsSolutionsTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getJLessonsSolutionsTabbedPane() {
		if (jLessonsSolutionsTabbedPane == null) {
			jLessonsSolutionsTabbedPane = new JTabbedPane();
			jLessonsSolutionsTabbedPane.addTab("Lesson", null, getJLessonTab(),
					null);
			jLessonsSolutionsTabbedPane.addTab("Solution", null,
					getJSolutionTab(), null);
		}
		return jLessonsSolutionsTabbedPane;
	}

	/**
	 * This method initializes jLessonTab
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJLessonTab() {
		if (jLessonTab == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.weighty = 1.0;
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 0;
			gridBagConstraints11.weightx = 1.0;
			jLessonTab = new JPanel();
			jLessonTab.setLayout(new GridBagLayout());
			jLessonTab.setBackground(Color.white);
			jLessonTab.setPreferredSize(new Dimension(457, 75));
			jLessonTab.add(getJLessonSplitPane(), gridBagConstraints11);
		}
		return jLessonTab;
	}

	/**
	 * This method initializes jSolutionTab
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJSolutionTab() {
		if (jSolutionTab == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.weightx = 1.0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 0;
			jSolutionTab = new JPanel();
			jSolutionTab.setLayout(new GridBagLayout());
			jSolutionTab.setBackground(Color.LIGHT_GRAY);
			jSolutionTab.add(getJSolutionButton(), gridBagConstraints1);
			jSolutionTab.add(getJSolutionEditorPane(), gridBagConstraints2);
			jSolutionTab.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusGained(java.awt.event.FocusEvent e) {

				}
			});
		}
		return jSolutionTab;
	}

	/**
	 * This method initializes jLessonTree1
	 * 
	 * @return javax.swing.JTree
	 */
	private JTree getJLessonTree() {
		if (jLessonTree == null) {
			LessonTreeBuilder treeBuilder = new LessonTreeBuilder();
			jLessonTree = treeBuilder.buildTree();
			jLessonTree
					.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
						public void valueChanged(
								javax.swing.event.TreeSelectionEvent e) {
							try {
								jLessonInfoEditorPane.setPage(LessonTreeBuilder
										.createLessonInfoURL(jLessonTree));
							} catch (IOException e2) {
								e2.getMessage();
							}
							jSolutionEditorPane.setVisible(false);
							jSolutionButton.setVisible(true);
							try {
								jSolutionEditorPane.setPage("file://");
							} catch (IOException e1) {
								e1.getMessage();
							}
						}
					});
		}
		return jLessonTree;
	}

	/**
	 * This method initializes jLessonInfoEditorPane
	 * 
	 * @return javax.swing.JEditorPane
	 */
	private JEditorPane getJLessonInfoEditorPane() {
		if (jLessonInfoEditorPane == null) {
			jLessonInfoEditorPane = new JEditorPane();
			jLessonInfoEditorPane.setEditable(false);
			jLessonInfoEditorPane.setPreferredSize(new Dimension(500, 585));
			jLessonInfoEditorPane.setContentType("text/html");

		}
		return jLessonInfoEditorPane;
	}

	/**
	 * This method initializes jSolutionButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJSolutionButton() {
		if (jSolutionButton == null) {
			jSolutionButton = new JButton();
			jSolutionButton
					.setText("Press This Button Only If You Really Need To See The Solution!");
			jSolutionButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseReleased(java.awt.event.MouseEvent e) {
					if (LessonTreeBuilder.isLessonDirectory(LessonTreeBuilder
							.getCurrentNodeParent(jLessonTree))) {
						jSolutionButton.setVisible(false);
						jSolutionEditorPane.setVisible(true);

						try {
							jSolutionEditorPane.setPage(LessonTreeBuilder
									.createSolutionURL(jLessonTree));
						} catch (IOException e1) {
							e1.getMessage();
						}

					}
				}
			});
		}
		return jSolutionButton;
	}

	/**
	 * This method initializes jSolutionEditorPane
	 * 
	 * @return javax.swing.JEditorPane
	 */
	private JEditorPane getJSolutionEditorPane() {
		if (jSolutionEditorPane == null) {
			jSolutionEditorPane = new JEditorPane();
			jSolutionEditorPane.setVisible(false);
			jSolutionEditorPane.setText("");
			jSolutionEditorPane.setEditable(false);
			jSolutionEditorPane.setContentType("text/html");

		}
		return jSolutionEditorPane;
	}

	/**
	 * This method initializes jConfigurationEditMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJConfigurationEditMenuItem() {
		if (jConfigurationEditMenuItem == null) {
			jConfigurationEditMenuItem = new JMenuItem();
			jConfigurationEditMenuItem.setText("Edit");
			jConfigurationEditMenuItem
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							ConfigureEditFrame editFrame;
							try {
								editFrame = new ConfigureEditFrame();
								editFrame.setVisible(true);
							} catch (FileNotFoundException e1) {
								showConfigErrorDialog();
							} catch (IOException e1) {
								showConfigErrorDialog();
							} catch (CorruptConfigException e1) {
								showConfigErrorDialog();
							}

						}
					});
		}
		return jConfigurationEditMenuItem;
	}

	/**
	 * This method initializes jAboutMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJAboutMenuItem() {
		if (jAboutMenuItem == null) {
			jAboutMenuItem = new JMenuItem();
			jAboutMenuItem.setText("About");
			jAboutMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent e) {
					AboutFrame aboutFrame = new AboutFrame();
					aboutFrame.setVisible(true);
				}
			});
		}
		return jAboutMenuItem;
	}

	/**
	 * This method initializes jToolsMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJToolsMenu() {
		if (jToolsMenu == null) {
			jToolsMenu = new JMenu();
			jToolsMenu.setText("Tools");
			jToolsMenu.add(getJToolsSendLocationMenuItem());
		}
		return jToolsMenu;
	}

	/**
	 * This method initializes jToolsSendLocationMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJToolsSendLocationMenuItem() {
		if (jToolsSendLocationMenuItem == null) {
			jToolsSendLocationMenuItem = new JMenuItem();
			jToolsSendLocationMenuItem.setText("Send Location To Device");
			jToolsSendLocationMenuItem
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							SendLocationFrame sendLocation = new SendLocationFrame();
							sendLocation.setVisible(true);
						}
					});
		}
		return jToolsSendLocationMenuItem;
	}

	public void showConfigErrorDialog() {
		JOptionPane.showMessageDialog(this, "Bad");
	}

	/**
	 * This method initializes jLessonSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getJLessonSplitPane() {
		if (jLessonSplitPane == null) {
			jLessonSplitPane = new JSplitPane();
			jLessonSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			jLessonSplitPane.setOneTouchExpandable(false);
			jLessonSplitPane.setBottomComponent(getJLessonStartPanel());
			jLessonSplitPane.setTopComponent(getJLessonInfoEditorPane());
		}
		return jLessonSplitPane;
	}

	/**
	 * This method initializes jLessonStartPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJLessonStartPanel() {
		if (jLessonStartPanel == null) {
			jLessonStartPanel = new JPanel();
			jLessonStartPanel.setLayout(null);
			jLessonStartPanel.add(getJStartWebServiceButton(), null);
			jLessonStartPanel.add(getJStartInEmulatorButton(), null);
			jLessonStartPanel.add(getJPushAppButton(), null);
		}
		return jLessonStartPanel;
	}

	/**
	 * This method initializes jStartWebServiceButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJStartWebServiceButton() {
		if (jStartWebServiceButton == null) {
			jStartWebServiceButton = new JButton();
			jStartWebServiceButton.setText("Start Web Service");
			jStartWebServiceButton.setLocation(new Point(552, 13));
			jStartWebServiceButton.setForeground(Color.black);
			jStartWebServiceButton.setBackground(Color.lightGray);
			jStartWebServiceButton.setSize(new Dimension(173, 29));
			jStartWebServiceButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						@SuppressWarnings("static-access")
						public void mouseReleased(java.awt.event.MouseEvent e) {
							WebServices service = new WebServices(jLessonTree);
							try {
								if (jStartWebServiceButton.getText()
										.startsWith("Start")) {
									service.startService();
									jStartWebServiceButton
											.setText("Stop Web Service");
									jStartWebServiceButton
											.setForeground(Color.red);
								} else {
									service.stopService();
									jStartWebServiceButton
											.setText("Start Web Service");
									jStartWebServiceButton
											.setForeground(Color.black);
								}
							} catch (Exception e1) {
							}
						}
					});
		}
		return jStartWebServiceButton;
	}

	/**
	 * This method initializes jStartInEmulatorButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJStartInEmulatorButton() {
		if (jStartInEmulatorButton == null) {
			jStartInEmulatorButton = new JButton();
			jStartInEmulatorButton.setText("Start In Emulator");
			jStartInEmulatorButton.setSize(new Dimension(173, 29));
			jStartInEmulatorButton.setLocation(new Point(36, 13));
			jStartInEmulatorButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							try {
								Emulator emulator = new Emulator();
								@SuppressWarnings("static-access")
								String devicePath = config
										.getVirtualDevicesPath();
								if (devicePath.equals("")) {
									showStartEmulatorEmptyFailure();
								} else {

									@SuppressWarnings("static-access")
									ArrayList<String> devices = emulator
											.getVirtualDevices(devicePath);
									String[] deviceArray = new String[devices
											.size()];
									deviceArray = devices.toArray(deviceArray);
									JOptionPane pane = new JOptionPane();
									String selection = (String) pane
											.showInputDialog(
													null,
													"Please select your device",
													"Device Selector",
													JOptionPane.QUESTION_MESSAGE,
													null, deviceArray,
													deviceArray[0]);
									ExecutorService execService = Executors
											.newFixedThreadPool(1);
									execService.execute(new EmulatorRunnable(
											selection));
								}

							} catch (FileNotFoundException e1) {
								e1.getMessage();
							} catch (IOException e1) {
								e1.getMessage();
							} catch (CorruptConfigException e1) {
								e1.getMessage();
							}
						}
					});
		}
		return jStartInEmulatorButton;
	}

	/**
	 * This method initializes jPushAppButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJPushAppButton() {
		if (jPushAppButton == null) {
			jPushAppButton = new JButton();
			jPushAppButton.setText("Push App To Device");
			jPushAppButton.setSize(new Dimension(173, 29));
			jPushAppButton.setLocation(new Point(288, 13));
			jPushAppButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseReleased(java.awt.event.MouseEvent e) {

					try {
						Emulator emulator = new Emulator();
						String appPath = emulator.getAppPath(jLessonTree);

						if (appPath.equals(""))
							showAppLaunchFailDialog();
						else {
							showSuccessPushing();
							emulator.pushAppOntoDevice(appPath);
						}
					} catch (FileNotFoundException e1) {
						e1.getMessage();
					} catch (IOException e1) {
						e1.getMessage();
					} catch (CorruptConfigException e1) {
						e1.getMessage();
					}
				}
			});
		}
		return jPushAppButton;
	}

	private void showAppLaunchFailDialog() {
		JOptionPane.showMessageDialog(this, Constants.APP_PUSH_FAILED);
	}

	private void showSuccessPushing() {
		JOptionPane.showMessageDialog(this, Constants.PUSHING_APP);
	}

	private void showStartEmulatorEmptyFailure() {
		JOptionPane.showMessageDialog(this, Constants.SPECIFY_SDK_LOCATION);
	}
}
